<?php
$type= $_GET['type']; 
$string = file_get_contents("prova.htm","r");
$WapHeader='<wml><head><meta http-equiv="cache-control" content="no-cache"/></head><card title="GRA" newcontext="true">';
$WapFooter = '</card></wml>';
$TableHeader = '<table columns="3"><tr><td>TRATTO</td><td>SUD</td><td>NORD</td></tr>';
if ($type=='color') {
    $TableLine = '<tr><td> tratto</td><td style="background-color: rgb(r_l, g_l, b_l)">str_l</td><td style="background-color: rgb(r_r, g_r, b_r)">str_r</td></tr>';
  } else {
    $TableLine = 'tratto  str_l  str_r<br>';
  }
$TableFooter = '</table><br/>LEGENDA<br/>V=90+Kmh, B=70-90Kmh, <br/>G=50-70kmh,A=30-50kmh,<br/>R=10-30kmh, N=0-10kmh<br/>';
$ColorValues=array(array(100,100,100),array(0,0,0),array(255,0,0),array(100,100,0),array(255,255,0),array(0,255,255),array(0,255,0));
$LetterValues=array('?','N','R','A','G','B','V');
// $SegmentValues=array('S.Donato Milanese - SUD','S.Donato Milanese - NORD','Tangenziale Est - SUD','Tangenziale Est - NORD','S.Giuliano Milanese - SUD','S.Giuliano Milanese - NORD','Tangenziale Ovest - SUD','Tangenziale Ovest - NORD','Melegnano - Binasco - SUD','Melegnano - Binasco - NORD',
//                      'Lodi - SUD','Lodi - NORD','Casalpusterlengo - SUD','Casalpusterlengo - NORD','Piacenza Nord - SUD','Piacenza Nord - NORD','>Piacenza Sud / Allaccio A21 - SUD','','','',
//                      '','','','','','','','','','',
//                      '','','','','','','','','','',
//                      '','','','','','','','','','',
//                      '','','','','','','','','','',
//                      '','');
print $WapHeader;
print 'Data copyright (C) of <a href="http://traffico.octotelematics.com" accesskey=0 title = "Octotelematics">Octotelematics</a><br>';
print "This is an *unofficial* wap frontend for Octotelematics data which allows<br>";
print "downloading traffic data on mobile devices at low charge, by means of reducing<br>";
print "the amount of transferred data.<br>";
print "Please show at least following lines if you want to display data on your<br>";
print "mobile application:<br><br>";
print '(C)Copyright Octotelematics: <a href="http://traffico.octotelematics.com" accesskey=0 title = "Octotelematics">http://traffico.octotelematics.com</a><br>';
print 'Sponsored by Linear assicurazioni: <a href="http://www.tribulinear.it/landing_octotelematics/" accesskey=1 title = "Linear">http://www.tribulinear.it/landing_octotelematics/</a><br>';
print '<br>Data for A1 "Milano-Napoli"<br>';

if ($type=='color') {
    print $TableHeader;
  } else {
    print " ";
  }
  
for ( $index=1; $index<=62; $index++){
  // Determine positions of numbers and letters for traffic data:
  $InizioSezione=strpos($string,'LD' . $index);
  $InizioVelSin=strpos($string,'lx',$InizioSezione)+2;
  $InizioVelDes=strpos($string,'rx',$InizioSezione)+2;
  
  // ********* Left value ***********
  // Get value for LEFT column:
  $ArrayIndex=intval(substr($string,$InizioVelSin,1));
  
  // Convert numbers and letters values to suitable values:
  $Letter = $LetterValues[$ArrayIndex];
  $r = $ColorValues[$ArrayIndex][0];
  $g = $ColorValues[$ArrayIndex][1];
  $b = $ColorValues[$ArrayIndex][2];
  
  // Prepare table line:
  $Line = $TableLine;
  $Line=str_replace('tratto',$index,$Line);
  $Line=str_replace('str_l',$Letter,$Line);
  $Line=str_replace('r_l',$r,$Line); 
  $Line=str_replace('g_l',$g,$Line); 
  $Line=str_replace('b_l',$b,$Line);
  
  // ********* Right value ***********
  // Get value for RIGHT column:
  $ArrayIndex=substr($string,$InizioVelDes,1);

  // Convert numbers and letters values to suitable values:
  $Letter = $LetterValues[$ArrayIndex];
  $r = $ColorValues[$ArrayIndex][0];
  $g = $ColorValues[$ArrayIndex][1];
  $b = $ColorValues[$ArrayIndex][2];
  
  // Prepare table line:
  $Line=str_replace('str_r',$Letter,$Line);
  $Line=str_replace('r_r',$r,$Line); 
  $Line=str_replace('g_r',$g,$Line); 
  $Line=str_replace('b_r',$b,$Line);

  $Line = $Line;
  
  print $Line ;

}

if ($type=='color') {
    print $TableFooter;
  } else {
    print " ";
  }
  
print "<br>Please contact jumpjack at libero dot it for informations.<br>";
print $WapFooter;
?>
